package com.fx.service.impl;

import cn.hutool.core.date.DateUtil;
import com.fx.entity.UserInfo;
import com.fx.entity.WalletInfo;
import com.fx.mapper.UserInfoMapper;
import com.fx.mapper.WalletInfoMapper;
import com.fx.service.UserInfoService;
import com.fx.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private WalletInfoMapper walletInfoMapper;

    @Override
    public UserInfo getById(String flId) {
        return userInfoMapper.getById(flId);
    }

    @Override
    public List<UserInfo> getList(Map<String, Object> map) {
        return userInfoMapper.queryList(map);
    }

    @Override
    public int getCount(Map<String, Object> map) {
        return userInfoMapper.queryCount(map);
    }

    @Override
    public int save(UserInfo userInfo) {
        String id = String.valueOf(idWorker.nextId());
        userInfo.setFlId(String.valueOf(idWorker.nextId()));
        userInfo.setFlRegisteredTime(DateUtil.now());
        WalletInfo walletInfo = new WalletInfo();
        walletInfo.setFlId(id);
        walletInfo.setFlWithdrawAmount(BigDecimal.ZERO);
        walletInfo.setFlAvailableAmount(BigDecimal.ZERO);
        walletInfo.setFlCreatedTime(DateUtil.now());
        walletInfo.setFlFrostAmount(BigDecimal.ZERO);
        walletInfo.setFlIncomeAmount(BigDecimal.ZERO);
        walletInfoMapper.save(walletInfo);
        userInfo.setFlWallet(id);
        return userInfoMapper.save(userInfo);
    }

    @Override
    public int updateById(UserInfo userInfo) {
        return userInfoMapper.updateById(userInfo);
    }

    @Override
    public int remove(String flId) {
        return userInfoMapper.remove(flId);
    }

    @Override
    public int batchRemove(String[] flIds) {
        return userInfoMapper.batchRemove(flIds);
    }

    @Override
    public List<UserInfo> getUserByLevel(String userId, Integer level) {
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        if(level == 1){
            userInfoList = getFirstUserList(userId);
        }else if(level == 2){
            userInfoList = getFirstUserList(userId);
            String[] ids = userConvertArr(userInfoList);
            userInfoList = getSecondUserList(ids);
        }else if(level == 3){
            userInfoList = getFirstUserList(userId);
            String[] ids = userConvertArr(userInfoList);
            List<UserInfo> secondUserList = getSecondUserList(ids);
            String[] secondIds = userConvertArr(secondUserList);
            userInfoList = getThirdUserList(secondIds);
        }else if(level == -1){
            List<UserInfo> firstUserInfoList = getFirstUserList(userId);
            userInfoList.addAll(firstUserInfoList);
            String[] ids = userConvertArr(firstUserInfoList);
            List<UserInfo> secondUserList = getSecondUserList(ids);
            userInfoList.addAll(secondUserList);
            String[] secondIds = userConvertArr(secondUserList);
            List<UserInfo> thirdUserList = getThirdUserList(secondIds);
            userInfoList.addAll(thirdUserList);
        }
        return userInfoList;
    }

    @Override
    public UserInfo getUserByOpenId(String openId) {
        return userInfoMapper.queryByOpenId(openId);
    }

    private List<UserInfo> getFirstUserList(String userId){
        List<UserInfo> userInfoList = userInfoMapper.queryFirstUserList(userId);
        return userInfoList;
    }

    private List<UserInfo> getSecondUserList(String[] ids){
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        if(ids.length != 0){
            userInfoList = userInfoMapper.queryNotFirstUserList(ids);
        }
        return userInfoList;
    }

    private List<UserInfo> getThirdUserList(String[] ids){
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        if(ids.length != 0){
            userInfoList = userInfoMapper.queryNotFirstUserList(ids);
        }
        return userInfoList;
    }

    private String[] userConvertArr(List<UserInfo> userInfoList){
        String[] ids = new String[userInfoList.size()];
        for (int i = 0; i <userInfoList.size() ; i++) {
            UserInfo userInfo = userInfoList.get(i);
            ids[i] = userInfo.getFlId();
        }
        return ids;
    }

}
